package com.yanchangapp.ui.activity.purchase;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputFilter;
import android.text.SpannableString;
import android.text.Spanned;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.decoration.RecycleViewDivider;
import com.luck.picture.lib.tools.ScreenUtils;
import com.yanchangapp.PsCreateListAdapter;
import com.yanchangapp.R;
import com.yanchangapp.adapter.PsCntCreateListAdapter;
import com.yanchangapp.adapter.SupplierAdapter;
import com.yanchangapp.adapter.WareHouseAdapter;
import com.yanchangapp.commom.LoadingType;
import com.yanchangapp.model.bean.ProductBean;
import com.yanchangapp.model.bean.ProductExtractBean;
import com.yanchangapp.model.bean.SupplierBean;
import com.yanchangapp.model.bean.WareHouseBean;
import com.yanchangapp.model.cache.UserCache;
import com.yanchangapp.ui.presenter.OrderCreateAtPresenter;
import com.yanchangapp.ui.view.OrderCreateAtView;
import com.yanchangapp.util.CashierInputFilter;
import com.yanchangapp.widget.BottomDialog;
import com.yanchangapp.widget.GoodsPriceDialog;
import com.yanchangapp.widget.SpannableMoney;
import com.zxl.baselib.baseapp.AppManager;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.formatter.BigDecimalUtils;
import com.zxl.baselib.util.time.TimeUtil;
import com.zxl.baselib.util.ui.ResHelper;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.util.window.KeyboardHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 采购 --
 * 新建采购订单 ---
 * <p>
 * 基本普通........
 */
public class POrderCreateActivity extends BaseActivity<OrderCreateAtView, OrderCreateAtPresenter> implements OrderCreateAtView, PsCreateListAdapter.ITotalAmountChanged {
    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.tv_publish_now)
    TextView tvPublishNow;
    @BindView(R.id.btnCreate)
    Button btnCreate;
    @BindView(R.id.tvProductSum)
    TextView tvProductSum;
    @BindView(R.id.tvTime)
    TextView tvTime;
    @BindView(R.id.tvDepartment)
    TextView tvDepartment;
    @BindView(R.id.tvManName)
    TextView tvManName;
    @BindView(R.id.tvSupplerTitle)
    TextView tvSupplerTitle;
    @BindView(R.id.ivSupplierArrow)
    ImageView ivSupplierArrow;
    @BindView(R.id.tvSupplierName)
    TextView tvSupplierName;
    @BindView(R.id.rlSupplerCompany)
    RelativeLayout rlSupplerCompany;
    @BindView(R.id.tvAreaTitle)
    TextView tvAreaTitle;
    @BindView(R.id.ivAreaArrow)
    ImageView ivAreaArrow;
    @BindView(R.id.tvWareHouse)
    TextView tvWareHouse;
    @BindView(R.id.rlWareHouse)
    RelativeLayout rlWareHouse;
    @BindView(R.id.tvChooseProduct)
    TextView tvChooseProduct;
    @BindView(R.id.tvProductEmpty)
    TextView tvProductEmpty;
    @BindView(R.id.rvProductList)
    RecyclerView mRvProductList;
    // 仓库地址 相关的信息
    private BottomDialog mBottomCangKuArea;
    // 仓库地址适配器
    private WareHouseAdapter mWareHouseAdapter;
    // 仓库地址数据层
    private List<WareHouseBean> mWareHouseBeans = new ArrayList<>();
    // 仓库地址RecyclerView
    private RecyclerView mWareHouseRv;
    // 加载中view
    private LinearLayout mWareLlLoading,mWareLlEmpty;
    private RelativeLayout mWareRlError;
    // 是否数据已经加载
    private boolean mIsWareHouseLoading = false;
    // 之前选中的index
    private int mWarePreIndex = -1;
    // 现在选中的index 判断是否选中了对应的index....
    private int mWareNowIndex = -1;
    private BottomDialog mBottomSupplier;
    private SupplierAdapter mSupplierAdapter;
    private List<SupplierBean> mSupplierBeans = new ArrayList<>();
    private RecyclerView mRvSupplier;
    private LinearLayout mSupplierLlLoading,mSupplierLlEmpty;
    private RelativeLayout mSupplierRlError;
    private boolean mIsSupplierLoading = false;
    private int mSupplierPreIndex = -1;
    private int mSupplierNowIndex = -1;

    private PsCreateListAdapter mPsCreateListAdapter;
    private List<ProductExtractBean> mExtractBeanList = new ArrayList<>();
    private double totalMoney;
    private EditText etNum;
    private Dialog mSingleDialog;
    private static final int REQUEST_PRODUCT_CODE = 0x0098;


    public static void show(Activity activity) {
        Intent intent = new Intent(activity, POrderCreateActivity.class);
        activity.startActivity(intent);
    }


    @Override
    protected void init() {

    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_porder_create;
    }

    @Override
    protected OrderCreateAtPresenter createPresenter() {
        return new OrderCreateAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvPublishNow.setVisibility(View.VISIBLE);
        tvPublishNow.setText("取消");
        tvToolbarTitle.setText("填写采购明细");
        setViewFromBefore();
        initAdapter();
        // 设置总数量
        setSumText();
    }

    @Override
    protected void onDestroy() {
        if (mBottomCangKuArea != null){
            mBottomCangKuArea.dismiss();
        }

        if (mBottomSupplier != null){
            mBottomSupplier.dismiss();
        }
        super.onDestroy();
    }

    private void initAdapter() {
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        manager.setSmoothScrollbarEnabled(true);
        manager.setAutoMeasureEnabled(true);
        mRvProductList.setHasFixedSize(true);
        mRvProductList.setNestedScrollingEnabled(false);
        mRvProductList.addItemDecoration(new RecycleViewDivider(
                this, LinearLayoutManager.HORIZONTAL, ScreenUtils.dip2px(this, 0.5f),
                ContextCompat.getColor(this, R.color.borderColorBase)));
        mRvProductList.setLayoutManager(manager);
        mPsCreateListAdapter = new PsCreateListAdapter(R.layout.item_ps_create_list,mExtractBeanList);
        mPsCreateListAdapter.setITotalAmountChanged(this);
        // 设置adapter...
        mRvProductList.setAdapter(mPsCreateListAdapter);
    }

    /**
     * 设置数据信息
     */
    private void setViewFromBefore() {
        tvTime.setText(TimeUtil.getTodayDayTime());
        tvDepartment.setText(UserCache.getDepartmentName());
        tvManName.setText(UserCache.getName());
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        ivToolbarNavigation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        tvPublishNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getAppManager().returnToActivity(POrderListActivity.class);
            }
        });

        rlWareHouse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showWareHouseSheet();
            }
        });

        // 仓库地址点击确认...
        rlSupplerCompany.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSupplierSheet();
            }
        });

        tvChooseProduct.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!tvChooseProduct.isSelected()){
                    UIUtils.showToast("请先选择供货单位");
                }else {
                    ProductNormalExtActivity.show(POrderCreateActivity.this,
                            mSupplierBeans.get(mSupplierNowIndex).getSupplierId(),mExtractBeanList);
                }
            }
        });

        mPsCreateListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.ivDelete){
                    mExtractBeanList.remove(position);
                    mPsCreateListAdapter.notifyItemRemoved(position);
                    mPsCreateListAdapter.notifyItemRangeChanged(position, mExtractBeanList.size() - position);
                    setSumText();
                    if (mExtractBeanList.size() == 0){
                        tvProductEmpty.setVisibility(View.VISIBLE);
                        mRvProductList.setVisibility(View.GONE);
                    }
                }else if (view.getId() == R.id.ivEdit){
                    showSetPriceDialog(POrderCreateActivity.this,position,mExtractBeanList.get(position).getPrice());
                }
            }
        });

        btnCreate.setOnClickListener(v -> {
            if (!isCheckOk()){
                return;
            }

            List<ProductBean> mProductLists = new ArrayList<>();
            for (int i=0;i<mExtractBeanList.size();i++){
                ProductExtractBean bean = mExtractBeanList.get(i);
                mProductLists.add(new ProductBean(
                        bean.getProductId(),bean.getSelectSize(),bean.getPrice(),
                        bean.getPrice() * bean.getSelectSize()));
            }

            mPresenter.actionCreateOrder(
                    mSupplierBeans.get(mSupplierNowIndex).getSupplierId(),
                    mWareHouseBeans.get(mWareNowIndex).getId()
                    ,totalMoney,
                    mProductLists);
        });
    }

    private boolean isCheckOk(){
        if (mSupplierNowIndex == -1){
            UIUtils.showToast("请选择供货单位");
            return false;
        }

        if (mWareNowIndex == -1){
            UIUtils.showToast("请选择仓库地址");
            return false;
        }


        if (mExtractBeanList.size() == 0){
            UIUtils.showToast("请进行品种选择");
            return false;
        }

//
//        for (int i=0;i<mExtractBeanList.size();i++){
//            ProductExtractBean productExtractBean = mExtractBeanList.get(i);
//            String price = String.valueOf(productExtractBean.getPrice());
//            if ("0".equals(price) || "0.0".equals(price) || "0.00".equals(price)) {
//                UIUtils.showToast("请输入有效单价");
//                return false;
//            }
//
//        }
        return true;
    }

    private void setAddProductEnable(){
        if (!tvChooseProduct.isSelected()){
            tvChooseProduct.setSelected(true);
        }
    }

    private void showSetPriceDialog(Context context,int position,double price) {
        mSingleDialog = new GoodsPriceDialog.Builder(context)
                .setPrice(price)
                .setEnsureListener(new GoodsPriceDialog.PriceEnsureListener() {
                    @Override
                    public void onEnsureClick(double _price) {
                        mExtractBeanList.get(position).setPrice(_price);
                        mPsCreateListAdapter.notifyItemChanged(position);
                        setSumText();
                        mSingleDialog.dismiss();
                    }
                }).build();
        mSingleDialog.show();
    }


    /**
     * 加载仓库地址的sheet
     */
    private void showWareHouseSheet(){
        if (mBottomCangKuArea == null){
            mBottomCangKuArea = new BottomDialog(this,0,true);
            View view = LayoutInflater.from(this).inflate(R.layout.bottom_cangku_area, null, false);
            mBottomCangKuArea.setContentView(view);
            // 设置背景为透明色 那么白色的就能呈现出来了
            mBottomCangKuArea.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet)
                    .setBackgroundColor(getResources().getColor(android.R.color.transparent));
            mWareHouseRv = view.findViewById(R.id.rvWareHouse);
            mWareLlEmpty = view.findViewById(R.id.llEmpty);
            mWareLlLoading = view.findViewById(R.id.llLoading);
            mWareRlError = view.findViewById(R.id.rlErrorNetwork);
            TextView mTvWareRefresh = view.findViewById(R.id.btnNextRefresh);
            TextView tvToolbarTitle = view.findViewById(R.id.tvToolbarTitle);
            tvToolbarTitle.setText("选择仓库地址");
            view.findViewById(R.id.closeBottom).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mBottomCangKuArea.hide();
                }
            });
            mWareHouseRv.addItemDecoration(new RecycleViewDivider(
                    this, LinearLayoutManager.HORIZONTAL, ScreenUtils.dip2px(this,0.5f), ContextCompat.getColor(this, R.color.borderColorBase)));
            mWareHouseRv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
            mWareHouseAdapter = new WareHouseAdapter(R.layout.item_checkbox_text,mWareHouseBeans);
            mWareHouseAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                    if (mWareNowIndex == i){
                        // 如果相等直接关闭
                        mBottomCangKuArea.hide();
                        return;
                    }
                    mWarePreIndex = mWareNowIndex;
                    if (mWarePreIndex != -1){
                        mWareHouseBeans.get(mWarePreIndex).setChecked(false);
                        mWareHouseAdapter.notifyItemChanged(mWarePreIndex);
                    }
                    mWareNowIndex = i;
                    mWareHouseBeans.get(mWareNowIndex).setChecked(true);
                    // 设置变化
                    mWareHouseAdapter.notifyItemChanged(mWareNowIndex);
                    // 设置仓库地址...
                    tvWareHouse.setText(mWareHouseBeans.get(mWareNowIndex).getName());
                    mBottomCangKuArea.hide();
                }
            });
            mTvWareRefresh.setOnClickListener(v -> loadWareHouseData());
            // 设置Adapter
            mWareHouseRv.setAdapter(mWareHouseAdapter);
        }
        // 加载数据
        loadWareHouseData();
        // 进行数据加载
        if (!isFinishing()){
            mBottomCangKuArea.show();
        }
    }

    /**
     * 加载供货单位sheet...
     */
    private void showSupplierSheet(){
        if (mBottomSupplier == null){
            mBottomSupplier = new BottomDialog(this,0,true);
            View view = LayoutInflater.from(this).inflate(R.layout.bottom_cangku_area, null, false);
            mBottomSupplier.setContentView(view);
            // 设置背景为透明色 那么白色的就能呈现出来了
            mBottomSupplier.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet)
                    .setBackgroundColor(getResources().getColor(android.R.color.transparent));
            mRvSupplier = view.findViewById(R.id.rvWareHouse);
            mSupplierLlEmpty = view.findViewById(R.id.llEmpty);
            mSupplierLlLoading = view.findViewById(R.id.llLoading);
            mSupplierRlError = view.findViewById(R.id.rlErrorNetwork);
            TextView mTvWareRefresh = view.findViewById(R.id.btnNextRefresh);
            TextView tvToolbarTitle = view.findViewById(R.id.tvToolbarTitle);
            tvToolbarTitle.setText("选择供货单位");
            view.findViewById(R.id.closeBottom).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mBottomSupplier.hide();
                }
            });
            mRvSupplier.addItemDecoration(new RecycleViewDivider(
                    this, LinearLayoutManager.HORIZONTAL, ScreenUtils.dip2px(this,0.5f), ContextCompat.getColor(this, R.color.borderColorBase)));
            mRvSupplier.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
            mSupplierAdapter = new SupplierAdapter(R.layout.item_checkbox_text,mSupplierBeans);
            mSupplierAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                    if (mSupplierNowIndex == i){
                        // 如果相等直接关闭
                        mBottomSupplier.hide();
                        return;
                    }
                    mSupplierPreIndex = mSupplierNowIndex;
                    if (mSupplierPreIndex != -1){
                        mSupplierBeans.get(mSupplierPreIndex).setChecked(false);
                        mSupplierAdapter.notifyItemChanged(mSupplierPreIndex);
                    }
                    mSupplierNowIndex = i;
                    mSupplierBeans.get(mSupplierNowIndex).setChecked(true);
                    // 设置变化
                    mSupplierAdapter.notifyItemChanged(mSupplierNowIndex);
                    // 设置仓库地址...
                    tvSupplierName.setText(mSupplierBeans.get(mSupplierNowIndex).getSupplierName());
                    mBottomSupplier.hide();
                    // 设置选这品种可见
                    setAddProductEnable();

                    resetProductsWithRefresh();
                }
            });
            mTvWareRefresh.setOnClickListener(v -> loadSupplierData());
            // 设置Adapter
            mRvSupplier.setAdapter(mSupplierAdapter);
        }
        // 加载数据
        loadSupplierData();
        // 进行数据加载
        if (!isFinishing()){
            mBottomSupplier.show();
        }
    }

    /**
     * 重置品种选择
     */
    private void resetProductsWithRefresh() {
        mExtractBeanList.clear();
        //设置变化....
        mPsCreateListAdapter.notifyDataSetChanged();
        setSumText();
        if (mExtractBeanList.size() == 0){
            tvProductEmpty.setVisibility(View.VISIBLE);
            mRvProductList.setVisibility(View.GONE);
        }

    }

    private void loadSupplierData(){
        if (!mIsSupplierLoading){
            setSupplierState(LoadingType.LOADING);
            mPresenter.loadSupplierData();
        }
    }

    private void setSupplierState(LoadingType loadingType){
        switch (loadingType){
            case SUCCESS:
                if (mRvSupplier.getVisibility() == View.GONE){
                    mRvSupplier.setVisibility(View.VISIBLE);
                }
                if (mSupplierRlError.getVisibility() == View.VISIBLE){
                    mSupplierRlError.setVisibility(View.GONE);
                }
                if (mSupplierLlLoading.getVisibility() == View.VISIBLE){
                    mSupplierLlLoading.setVisibility(View.GONE);
                }
                if (mSupplierLlEmpty.getVisibility() == View.VISIBLE){
                    mSupplierLlEmpty.setVisibility(View.GONE);
                }
                break;
            case LOADING:
                if (mRvSupplier.getVisibility() == View.VISIBLE){
                    mRvSupplier.setVisibility(View.GONE);
                }
                if (mSupplierRlError.getVisibility() == View.VISIBLE){
                    mSupplierRlError.setVisibility(View.GONE);
                }
                if (mSupplierLlLoading.getVisibility() == View.GONE){
                    mSupplierLlLoading.setVisibility(View.VISIBLE);
                }
                if (mSupplierLlEmpty.getVisibility() == View.VISIBLE){
                    mSupplierLlEmpty.setVisibility(View.GONE);
                }
                break;
            case EMPTY:
                if (mRvSupplier.getVisibility() == View.VISIBLE){
                    mRvSupplier.setVisibility(View.GONE);
                }
                if (mSupplierRlError.getVisibility() == View.VISIBLE){
                    mSupplierRlError.setVisibility(View.GONE);
                }
                if (mSupplierLlLoading.getVisibility() == View.VISIBLE){
                    mSupplierLlLoading.setVisibility(View.GONE);
                }
                if (mSupplierLlEmpty.getVisibility() == View.GONE){
                    mSupplierLlEmpty.setVisibility(View.VISIBLE);
                }
                break;
            case ERROR:
                if (mRvSupplier.getVisibility() == View.VISIBLE){
                    mRvSupplier.setVisibility(View.GONE);
                }
                if (mSupplierRlError.getVisibility() == View.GONE){
                    mSupplierRlError.setVisibility(View.VISIBLE);
                }
                if (mSupplierLlLoading.getVisibility() == View.VISIBLE){
                    mSupplierLlLoading.setVisibility(View.GONE);
                }
                if (mSupplierLlEmpty.getVisibility() == View.VISIBLE){
                    mSupplierLlEmpty.setVisibility(View.GONE);
                }
                break;
        }
    }

    private void loadWareHouseData(){
        if (!mIsWareHouseLoading){
            setWareSheetState(LoadingType.LOADING);
            mPresenter.loadWareHouseData();
        }
    }


    private void setWareSheetState(LoadingType loadingType){
        switch (loadingType){
            case SUCCESS:
                if (mWareHouseRv.getVisibility() == View.GONE){
                    mWareHouseRv.setVisibility(View.VISIBLE);
                }
                if (mWareRlError.getVisibility() == View.VISIBLE){
                    mWareRlError.setVisibility(View.GONE);
                }
                if (mWareLlLoading.getVisibility() == View.VISIBLE){
                    mWareLlLoading.setVisibility(View.GONE);
                }
                if (mWareLlEmpty.getVisibility() == View.VISIBLE){
                    mWareLlEmpty.setVisibility(View.GONE);
                }
                break;
            case LOADING:
                if (mWareHouseRv.getVisibility() == View.VISIBLE){
                    mWareHouseRv.setVisibility(View.GONE);
                }
                if (mWareRlError.getVisibility() == View.VISIBLE){
                    mWareRlError.setVisibility(View.GONE);
                }
                if (mWareLlLoading.getVisibility() == View.GONE){
                    mWareLlLoading.setVisibility(View.VISIBLE);
                }
                if (mWareLlEmpty.getVisibility() == View.VISIBLE){
                    mWareLlEmpty.setVisibility(View.GONE);
                }
                break;
            case EMPTY:
                if (mWareHouseRv.getVisibility() == View.VISIBLE){
                    mWareHouseRv.setVisibility(View.GONE);
                }
                if (mWareRlError.getVisibility() == View.VISIBLE){
                    mWareRlError.setVisibility(View.GONE);
                }
                if (mWareLlLoading.getVisibility() == View.VISIBLE){
                    mWareLlLoading.setVisibility(View.GONE);
                }
                if (mWareLlEmpty.getVisibility() == View.GONE){
                    mWareLlEmpty.setVisibility(View.VISIBLE);
                }
                break;
            case ERROR:
                if (mWareHouseRv.getVisibility() == View.VISIBLE){
                    mWareHouseRv.setVisibility(View.GONE);
                }
                if (mWareRlError.getVisibility() == View.GONE){
                    mWareRlError.setVisibility(View.VISIBLE);
                }
                if (mWareLlLoading.getVisibility() == View.VISIBLE){
                    mWareLlLoading.setVisibility(View.GONE);
                }
                if (mWareLlEmpty.getVisibility() == View.VISIBLE){
                    mWareLlEmpty.setVisibility(View.GONE);
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK){
            switch (requestCode){
                case REQUEST_PRODUCT_CODE:
                    mExtractBeanList.clear();
                    mExtractBeanList.addAll((List<ProductExtractBean>)data.getExtras().getSerializable("data"));
                    mPsCreateListAdapter.notifyDataSetChanged();
                    if (tvProductEmpty.getVisibility() == View.VISIBLE){
                        tvProductEmpty.setVisibility(View.GONE);
                    }

                    if (mRvProductList.getVisibility() == View.GONE){
                        mRvProductList.setVisibility(View.VISIBLE);
                    }
                    setSumText();
                    break;
                default:
                    break;
            }
        }
    }

    private void setSumText(){
        String totalMoney = BigDecimalUtils.keep2Decimal(calculateNum());
        int len = totalMoney.length();
        SpannableString spannableString = new SpannableString(ResHelper.getString(R.string.down_order_sum,
                String.valueOf(mExtractBeanList.size()),totalMoney));
        int totalLength = spannableString.length();
        spannableString.setSpan(new SpannableMoney(this),
                totalLength-len-1,totalLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvProductSum.setText(spannableString);
    }

    private double calculateNum(){
        double sum = 0;
        for (int i=0;i<mExtractBeanList.size();i++){
            ProductExtractBean bean = mExtractBeanList.get(i);
            sum += bean.getPrice() * bean.getSelectSize();
        }
        totalMoney = sum;
        return sum;
    }

    @Override
    public void queryWareHouseSuccess(List<WareHouseBean> mWareBeanList) {
        mIsWareHouseLoading = true;
        if (mWareBeanList == null || mWareBeanList.size() == 0){
            // 数据为空的情况
            setWareSheetState(LoadingType.EMPTY);
        }else {
            setWareSheetState(LoadingType.SUCCESS);
            if (mWareHouseAdapter != null && mBottomCangKuArea.isShowing()){
                mWareHouseBeans.addAll(mWareBeanList);
                mWareHouseAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void queryWareHouseFailure(String err) {
        setWareSheetState(LoadingType.ERROR);
        mIsWareHouseLoading = false;
        UIUtils.showToast(err);
    }

    @Override
    public void querySupplierSuccess(List<SupplierBean> mSupplierBeanList) {
        mIsSupplierLoading = true;
        if (mSupplierBeanList == null || mSupplierBeanList.size() == 0){
            // 数据为空的情况
            setSupplierState(LoadingType.EMPTY);
        }else {
            setSupplierState(LoadingType.SUCCESS);
            if (mSupplierAdapter != null && mBottomSupplier.isShowing()){
                mSupplierBeans.clear();
                mSupplierBeans.addAll(mSupplierBeanList);
                mSupplierAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void createOrderSuccess(String orderID) {
        mRxManager.post(POrderListActivity.REFRESH_LIST,true);
        POrderEnsureActivity.show(POrderCreateActivity.this,orderID);
    }

    @Override
    public void createOrderFailure(String err) {
        UIUtils.showToast(err);
    }

    @Override
    public void querySupplierFailure(String err) {
        setSupplierState(LoadingType.ERROR);
        mIsSupplierLoading = false;
        UIUtils.showToast(err);
    }

    @Override
    public void justCalculateTotalPrice() {
        setSumText();
    }

}
